//Cyclone-Portal
//GAM 450 Final Project
//Gregory Dillon, Brian Gates, Kyle Chmielewski
//Portal.cpp
//implementation file for the portal class

#include "portal.h"
#include <math.h>

//static define for portal radius
#define RADIUS 3

//code for Portal

//Portal::Portal()
//constructor
Portal::Portal()
{
   szfmt(this->namePortal,
         sizeof(this->namePortal),
         "Portal%d",
         1); // unique name
   this->tLast=qeTimeFrame(); // time of creation
} // Portal::Portal()

//Portal::Portal()
//overloaded constructor
Portal::Portal(int i)
{
   szfmt(this->namePortal,
         sizeof(this->namePortal),
         "Portal%d",
         i); // unique name
   this->tLast=qeTimeFrame(); // time of creation
} // Portal::Portal()


//Portal::zap()
//qe object delete
void Portal::zap()
{
   delete this; // free memory
} // Portal::zap()


//Portal::update()
//update Portal object
int Portal::update()
{
   return 0;
} // Portal::update()


//Portal::draw()
//draws Portal object
int Portal::draw(void)
{
   glPushMatrix();
   flt32 radius = RADIUS;        //get radius of portal
   const float DEG2RAD = 3.14159/180;     //convert degree to radians
   glColor3f(this->colorPortal[0], this->colorPortal[1], this->colorPortal[2]);     //color the portal
   glTranslatef(xyzPortal.x, xyzPortal.y, xyzPortal.z);     //move the portal to position

   glBegin(GL_LINE_LOOP);
 
   //Draw the loop of the portal
   for (int i=0; i < 360; i++)
   {
      float degInRad = i*DEG2RAD;
      glVertex3f(cos(degInRad)*radius, sin(degInRad)*radius, 0);
   }  // for
   
   glEnd();
   glPopMatrix();
   
   return 0;
} // Portal::draw()

//Portal::drawNoral()
//draw the normal vector of the vector for a visual confirmation
int Portal::drawNormal()
{
   glColor3f(1, 0, 1);     //draw the normal vector purple
   glPushMatrix();
   glBegin(GL_LINES);
      //set the endpoints of the normal from the portal to a set distance in space
      Vec3 tempNormal = normal + xyzPortal;
      glVertex3f(tempNormal.x,tempNormal.y,tempNormal.z);
      glVertex3f(xyzPortal.x, xyzPortal.y, xyzPortal.z);   
   glEnd();
   glPopMatrix();
   return 0;
}  // Portal::drawNormal()


//Portal::spherePlaneCollision
//Calculates collision between the particles and portal
int Portal::spherePlaneCollision(ParticleSystem *object)
{
   //Get the positions of the particles and portal
   Vector3 particle1 = object->getPosition(0);
   Vector3 particle2 = object->getPosition(1);
   Vec3 sphere = xyzPortal;

   // Calculates the point midway between particle 1 and particle 2

   int success = 0;
   // Check if any of the particles or the midpoint are in the sphere
   for (int i = 0; i <3; i++)
   {
      if ((object->getPosition(i).x > sphere.x-RADIUS)&&(object->getPosition(i).x < sphere.x+RADIUS))
      {
         if ((object->getPosition(i).y > sphere.y-RADIUS)&&(object->getPosition(i).y < sphere.y+RADIUS))
         {
            if ((object->getPosition(i).z > sphere.z-RADIUS)&&(object->getPosition(i).z < sphere.z+RADIUS))
            {
               success = 1;
               break;
            }  // if
         }  // if
      }  // if
      qePrintf("\n");
   }  // for

   //get the normal of the vector
   Vector3 norm = vecToVector(normal);
   Vector3 xyzPosition = vecToVector(xyzPortal);
   
   //Check which direction the the particle is entering the portal
   if  (!((object->getPosition(0)*(norm) > xyzPosition*(norm))
       && (object->getPosition(1)*(norm) < xyzPosition*(norm))
       || (object->getPosition(0)*(norm) < xyzPosition*(norm))
       && (object->getPosition(1)*(norm) > xyzPosition*(norm))))
   {
      success = 0;
   }  // if

   return success;
}  //Portal::spherePlaneCollision

//vecToVector
//convert a Vec3 to Vector3
Vector3 vecToVector(Vec3 v)
{
   Vector3 v3 = Vector3(v.x,v.y,v.z);
   return v3;
}  // vecToVector